package com.lg.cms.shiro.realm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.cms.dto.PermissionDTO;
import com.lg.cms.dto.ShiroUsernamePasswordToken;
import com.lg.cms.entity.SuperUser;
import com.lg.cms.service.PermissionService;
import com.lg.cms.service.RolePermissionService;
import com.lg.cms.service.SuperUserService;
import com.lg.cms.vo.admin.PermissionVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: 超级用户管理实现类
 * @Author: wuyuhang
 * @create: 2020-09-28 14:49
 */
@Slf4j
public class AdminRealm extends AuthorizingRealm {

    @Autowired
    RolePermissionService rolePermissionService;
    @Autowired
    SuperUserService superUserService;
    @Autowired
    PermissionService permissionService;

    @Override
    public String getName() {
        return "admin";
    }

    /**
     * 授权(权限)赋予用户权限
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户信息
        Subject subject = SecurityUtils.getSubject();
        //获取用户对象
        SuperUser superUser = (SuperUser) subject.getPrincipal();

        Set<String> roles = new HashSet<String>();
        roles.add("admin");
        //创建权限, 角色对象
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
        //获取角色权限列表,为登录用户赋予权限
        List<PermissionVO> permissionVOList = permissionService.listByRoleId(superUser.getRoleId());
        for(PermissionVO vo: permissionVOList){
            if(StringUtils.isNotBlank(vo.getPermissionPerm())){
                info.addStringPermission(vo.getPermissionPerm());
            }
        }
        return info;
    }

    /**
     * 认证(登录)
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     * 智能抛出两个异常, 用户名错误或密码错误的异常, controller捕获即可
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.info("进入超级用户认证器");
        //获取带用户名密码的token
        ShiroUsernamePasswordToken token = (ShiroUsernamePasswordToken) authenticationToken;
        String type = token.getUserType();
        SuperUser superUser = superUserService.getOne(new QueryWrapper<SuperUser>().eq("username", token.getUsername()));
        //如果用户名存在, 进入验证密码
        if(superUser != null){
            //实体类对象, 密码, realm名
            return new SimpleAuthenticationInfo(superUser, superUser.getPassword(), getName());
        }
        return null;
    }
}